var restify= require('restify')
,app= restify.createServer()
,config=require('../config')
,path= require('path')
,users = require('./users')
,weixin = require('./weixin')
,zsh = require('./zsh')

app.use(restify.queryParser())
app.use(restify.CORS())
app.use(restify.fullResponse())
app.use(restify.gzipResponse())
app.use(restify.acceptParser(app.acceptable))
app.get('/weixin_token',weixin.token)
app.get('/zsh_mrzc',zsh.mrzc)
app.get('/', function (req, res, next){
  var data = require('fs').readFileSync(path.join(__dirname,'..','public', 'index.html'));
  res.status(200);
  res.header('Content-Type', 'text/html');
  res.end(data.toString());
})
app.get(/^.*\.(html|png|svg|css|js|ico)$/,restify.serveStatic({directory: path.join(__dirname,'..','public')}));
app.get(/.*/,function(req, res,next){
	var ua = req.headers['user-agent'],detector=require('detector')
	require('fs').writeFile('ua.json',JSON.stringify(detector.parse(ua)),function(e){})
	if(req.headers.sid===undefined){
		res.header('Location','/#'+req.path)
        return res.send(302);
	}
	return next()
})
app.get('/login',users.loginView)
app.post('/login',users.loginAction)


app.listen(8081,process.env.OPENSHIFT_NODEJS_IP,function(){
	console.log('Server started on %s:%s',process.env.OPENSHIFT_NODEJS_IP,8081)
})
